﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Config;
using AMM.Business.Project;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Project;
using System.Windows.Forms;

namespace AMM.UserControls {
    public partial class BOMEditControl : BaseEditControl {

        private FlexBindingSource<BOM> _fbs;
        private MaterialProcessor _materialProcessor;

        public override object BaseEditModel {
            get {
                this._fbs.CurrentItem.BOMDetails = this.dgvMaterialEdit.GetDataSourceEx<BOMDetail>();
                return this._fbs.CurrentItem;
            }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<BOM>(); }
                this._fbs.CurrentItem = (BOM)value;
            }
        }

        public BOMEditControl() {
            this.InitializeComponent();

            this.MaterialSource.DataSource = new BasicsProcessor().FetchEnabledBasicsByType(BasicsType.MaterialSource);
            this.MaterialSource.DisplayMember = "Name";
            this.MaterialSource.ValueMember = "BasicsId";
        }

        private void BindMaterial() {
            MaterialSearchModel searchModel = new MaterialSearchModel() { IsEnabled = true };
            MaterialProcessor materialProcessor = new MaterialProcessor(int.MaxValue);
            List<MaterialViewModel> materialList = materialProcessor.FetchMaterialViewModelList(searchModel);
            this.Material.DisplayMemberEx = "Code";
            this.Material.ValueMemberEx = "Key";
            this.Material.HeightEx = 300;
            this.Material.WidthEx = 400;
            this.Material.PageSizeEx = 20;
            this.Material.DataBind(new MaterialViewModel(), x => new { x.MaterialType, x.Code, x.Name, x.Remarks }, materialList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.cbIsEnabled, x => x.Checked, y => y.IsEnabled);
            this._fbs.CreateBinding(this.tbDescription, x => x.Text, y => y.Description);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");
        }

        private void CustomerEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<BOM>(); }
            this._materialProcessor = new MaterialProcessor();

            this.BindMaterial();
            this.CreateFormBindings();
            this.BindValidation();

            this.dgvMaterialEdit.DataSourceEx = this._fbs.CurrentItem.BOMDetails.OrderBy(x => x.Material.Code).ToList();
            this.lblStatus.Text = MethodHelper.GetEnumsDescription(typeof(AuditStatus), this._fbs.CurrentItem.BOMStatus);
        }

        private void dgvMaterialEdit_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
            if (this.dgvMaterialEdit.Columns[e.ColumnIndex].Name == "Material") {
                int materialId = Convert.ToInt32(this.dgvMaterialEdit.Rows[e.RowIndex].Cells["Material"].Value);
                Model.Material material = this._materialProcessor.FetchMaterialById(materialId);
                this.dgvMaterialEdit.Rows[e.RowIndex].Cells["MaterialName"].Value = material.Name;
                this.dgvMaterialEdit.Rows[e.RowIndex].Cells["MaterialDescription"].Value = material.Remarks;
            }
        }
    }
}
